package com.tang.hawkmusic.fragment;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.tang.hawkmusic.R;
import com.tang.hawkmusic.bean.music.LocalMusic;

import java.io.IOException;
import java.util.ArrayList;

public class PlayerFragment extends Fragment {
    ArrayList<LocalMusic> localMusics;
    MediaPlayer player;
    int position=0;
    boolean isLocal=false;
    //界面的一些按钮
    private ImageView playModeBtn,lastBtn,playBtn,nextBtn,playListMenu;
    public PlayerFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_player, container, false);
        player = new MediaPlayer();
        playModeBtn=view.findViewById(R.id.play_mode_btn);
        lastBtn=view.findViewById(R.id.lastBtn);
        playBtn=view.findViewById(R.id.playBtn);
        nextBtn=view.findViewById(R.id.nextBtn);
        playListMenu=view.findViewById(R.id.play_list_menu);
        initButton();
        return view;
    }

    public ArrayList<LocalMusic> getLocalMusics() {
        return localMusics;
    }

    public void setLocalMusics(ArrayList<LocalMusic> localMusics) {
        this.localMusics = localMusics;
    }

    public void initButton(){
       lastBtn.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               if (position>0){
                   position--;
                   //如果是本地播放本地
                   if (isLocal){
                       player.reset();
                       try {
                           player.setDataSource(getContext(), Uri.parse(localMusics.get(position).getPath()));
                           player.prepare();
                           player.start();
                       } catch (IOException e) {
                           e.printStackTrace();
                       }
                   }else {
                       //TODO:播放网络音乐
                   }

               }

           }
       });
       playBtn.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               if (player.isPlaying()){
                  player.pause();
                  playBtn.setImageResource(R.drawable.ic_pause);
                  //playBtn.setImageDrawable(R.drawable.ic_pause);
               } else {
                   player.start();
                   playBtn.setImageResource(R.drawable.ic_play);
               }
           }
       });
       nextBtn.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               if (isLocal)
                   if (position<localMusics.size()-1){
                       position++;
                       player.reset();
                       try {
                           player.setDataSource(getContext(),Uri.parse(localMusics.get(position).getPath()));
                           player.prepare();
                           player.start();
                       } catch (IOException e) {
                           e.printStackTrace();
                       }
                   }
           }
       });
    }

    public void setPosition(int position) {
        this.position = position;
    }

    public void setLocal(boolean local) {
        isLocal = local;
    }
    public void beginPlay(){
        player.reset();
        try {
            player.setDataSource(getContext(),Uri.parse(localMusics.get(position).getPath()));
            player.prepare();
            player.start();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}